import 'package:app/utils/sp_utils.dart';
import 'package:app/view_model/login_model.dart';
import 'package:app/widget/provider_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'tap.dart';

class LogInPage extends StatefulWidget {
  @override
  _LogInPageState createState() => _LogInPageState();
}

class _LogInPageState extends State<LogInPage> {

  bool check=false;
  bool flag=false;
  String name="";
  String num="";
  String ps="";
  SPUtils sp=new SPUtils();
@override
  void initState() {
    // TODO: implement initState
    super.initState();

  }
  @override
  Widget build(BuildContext context) {
//    sp.getBool("check").then((value){
//        check=value??false;
//    });
    ScreenUtil.init(context, width: 750, height: 1334);
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white30,
        leading: IconButton(
          color: Colors.black,
            icon: Icon(

              CupertinoIcons.back,
              size: ScreenUtil().setSp(40),
            ),
            onPressed: () {}),
        actions: <Widget>[
          Container(
            alignment: Alignment.center,
            padding: EdgeInsets.only(right: ScreenUtil().setSp(30)),
            child: InkWell(
                onTap: () {},
                child: Text(
                  "注册",
                  style: TextStyle(fontSize: ScreenUtil().setSp(40),color: Colors.black),
                )),
          ),
        ],
      ),
      body: ProviderWidget<LoginModel>(model: LoginModel(), builder: (context,model,child){
        return Container(
          margin: EdgeInsets.only(left: ScreenUtil().setWidth(125)),
          alignment: Alignment.center,
          width: ScreenUtil().setWidth(500),
          child: Column(
            children: <Widget>[
              SizedBox(height: ScreenUtil().setHeight(220)),
              getInPutName(),
              SizedBox(height: ScreenUtil().setHeight(20)),
              getInPutNum(),
              SizedBox(height: ScreenUtil().setHeight(20)),
              getInPutPs(),
              Row(
                children: <Widget>[
                  Checkbox(value: check, onChanged: (value){
                    setState(() {
                      this.check=value;
                    });
                  }),
                  Text("记住我")
                ],
              ),

              Container(
                width: ScreenUtil().setWidth(500),

                child: RaisedButton(onPressed: (){
                  model.getHttpData(name,num,ps).then((value){
                    if(value){
                      flag=true;
                      setSP();
                      Navigator.of(context).push( MaterialPageRoute(builder: (context)=>Tap()));
                    }else{
                    }
                  });
                },
                  color: Theme.of(context).accentColor,
                  child: Text("提交"),
                ),
              )

            ],
          ),
        );
      }),
    );
  }

  void setSP() {
    sp.setBool("flag", flag);
    sp.setBool("check", check);
    sp.setUserInfo("name", name);
    sp.setUserInfo("num", num);
    sp.setUserInfo("ps", ps);
  }
  Widget getInPutName(){
     sp.getUserInfo("name").then((value)=>name=value??"");
    return Container(
    //  height: ScreenUtil().setHeight(100),
        alignment: Alignment.center,
      child: TextField(
        controller: TextEditingController.fromValue(TextEditingValue(
          text: name
        )),
        decoration: InputDecoration(
     //     contentPadding: EdgeInsets.symmetric(vertical: 10),
          fillColor: Colors.black12,
          filled: true,
          border: InputBorder.none,
          prefixIcon: Icon(Icons.person),
          hintText: "企业编号",
        ),
        onChanged: (value){
          name=value;
        },
      ),
    );
  }

  Widget getInPutNum(){
    sp.getUserInfo("num").then((value)=>num=value??"");
    return Container(
   //   height: ScreenUtil().setHeight(100),
      alignment: Alignment.center,

      child: TextField(
        controller: TextEditingController.fromValue(TextEditingValue(
            text: num
        )),
        decoration: InputDecoration(
        //  labelText: sp.getUserInfo("num").toString(),
          fillColor: Colors.black12,
          filled: true,
          border: InputBorder.none,
          prefixIcon: Icon(Icons.person),
          hintText: "用户名",
        ),
        onChanged: (value){
          num=value;
        },
      ),
    );
  }
  Widget getInPutPs(){
    sp.getUserInfo("ps").then((value)=>ps=value??"");
    return Container(
     // height: ScreenUtil().setHeight(100),
      alignment: Alignment.center,

      child: TextField(
        controller: TextEditingController.fromValue(TextEditingValue(
            text: ps
        )),
        decoration: InputDecoration(
       //   labelText: sp.getUserInfo("ps").toString(),
          border: InputBorder.none,
          fillColor: Colors.black12,
          filled: true,
          prefixIcon: Icon(Icons.https),
          suffixIcon: Icon(Icons.keyboard),
          hintText: "密码",
        ),
        onChanged: (value){
          ps=value;
        },
      ),
    );
  }

}
